import { Injectable } from '@angular/core';
import {FrameContext,BindingData,Repository} from '@farris/devkit';
import { CommandService,DialogService } from '@farris/command-services';
import { ModalOptions } from '@farris/ui-modal';
import {  BeActionService, FormMessageService } from '@farris/command-services';
import { BefRepository, ResponseInfo } from '@farris/bef';
import { tap,switchMap } from 'rxjs/operators'

@Injectable()
export class SbssLuPlanFrmControllerService {
  constructor(
    private dialogService: DialogService,
    private frameContext: FrameContext,
    private commandService: CommandService,
    private beActionService: BeActionService,
    private messageService: FormMessageService
  ) {}


  /**
   * 选择性弹窗打开
   * @remarks 
   * @param params 
   * @returns 
   */
  openModelByChoose(){
    let dataGrid = this.frameContext.appContext.componentManager.get(['data-grid-component','dataGrid']);
    let currentId = this.frameContext.bindingData.list.currentId
    let checkedsData = dataGrid.checkeds

    let options: ModalOptions = {"width":1200,"height":800,"title":"编辑润滑计划","areaResponse":true}
    let modeId = 'modalcontainer_7054'
    let params = {"action":"LoadAndEdit1","id":currentId,plandIds:[]}

    if(checkedsData.length == 1){
      // 只选择一条
      params = {"action":"LoadAndEdit1","id":checkedsData[0].id,plandIds:[]}
    }else if(checkedsData.length > 1) {
      // 选了很多条
      let checkIds = checkedsData.map(i=>i.id)
      modeId = 'modalcontainer_2272'
      params = {"action":"Filter1","id":'',plandIds:checkIds}
    }

    // 打开弹窗
    this.dialogService.openModal(options,modeId,params)
  }

  /**
   * 计划下达
   * @param planId 任务ID
   * @remarks 
   * @returns 
   */
  planRelease(id){
      if (id.length < 1){
        this.messageService.error("请勾选计划!")
        return
      }
      const rep = this.frameContext.repository as BefRepository<any>;
      const repInfo = rep.restService.buildRequestInfo();
      const actionUri = 'jhxd';
      const body = {
        ids: id,
        requestInfo: repInfo
      }
      const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
      return action$.pipe(
        tap((responseInfo: ResponseInfo) => {
          this.messageService.info("任务已生成!");
        }))
  }
}
